package day07

/**
 * set 集合
 *  与list集合区别：
 *      1.list 允许有重复的元素，set不允许重复元素
 * 创建set,通过setof
 *
 */
fun main() {

    val set = setOf("jack", "marry", "jerry", "jack")
    //安全操作获取元素
    val elementAtOrElse = set.elementAtOrElse(6){"空元素"}
    val s = set.elementAtOrNull(6) ?: "此元素为null"
    println(elementAtOrElse)
    println(s)

    //可变set集合 mutableset

    val mutableSetOf = mutableSetOf("jack", "marry", "jerry", "jacky","marry")
//    mutableSetOf += "marry"
//    mutableSetOf -= "jack"
//    println(mutableSetOf)
//    mutableSetOf.removeIf{it=="jack"}
//    println(mutableSetOf)

    //TODO: list 转set 去重元素

    val list3 = listOf("jack", "marry", "jerry", "jacky", "jack", "marry")
//    list3.toSet()
//        .toList()
//    println(list3)

    //快捷函数 distinct  去除重复元素
    val distinct = list3.distinct()
    val distinct1 = mutableSetOf.distinct()
    println(distinct)
    println(distinct1)


}